/**
 * 进入页面后第一次请求
 */
function firstRequest() {
	let url = location.search;
	let theRequest = new Object();
	if (url.indexOf("?") != -1) {
		let str = url.substr(1);
		let paramMap = getParamFromURL(str);
		let requestUrl = urls.cliUrl + "article/" + paramMap.id;
		$.ajax({
			type: "get",
			url: requestUrl,
			async: true,
			xhrFields: {
				withCredentials: true
			},
			success: function(res) {
				if (res.code === 0) {
					$('#articleId').val(res.data.id);
					$('#title').html(res.data.title);
					$('#author').html(res.data.author);
					$('#author').html(res.data.author);
					$('#pageView').html(res.data.pageView);
					$('#createTime').html(timestampToDateTime(res.data.createTime));
					$('#createDay').html(timestampToDay(res.data.createTime));
					$('#createMonth').html(timestampToMonth(res.data.createTime));
					$('#createYear').html(timestampToYear(res.data.createTime));
					$('#content').html(res.data.htmlContent);
					if (res.data.sourceUrl != null) {
						$('#sourceUrl').html("本文网址：<a href='" + res.data.sourceUrl + "' target='_blank'>" + textLenFormat(res.data.sourceUrl,
							50) + "</a>");
					}
				} else {
					$("#content").html("<h2><b>服务器异常!</b></h2>");
				}
			}
		});
		$.ajax({
			type: "get",
			url: urls.cliUrl + "comment/list/" + paramMap.id,
			async: true,
			xhrFields: {
				withCredentials: true
			},
			success: function(res) {
				if (res.code === 0) {
					$('#blog-comment').html("");
					for(let ct of res.data){
						$('#blog-comment').append("<li class='zoomIn article'><div class='comment-parent'><a name='remark-1'></a><img src='" + ct.headPictureLink + "'/><div class='info'><span class='username'>" + ct.username + "</span><span id='commentTime'>" + timestampToDateTime(ct.createTime) + "</span></div><div class='comment-content'>" + ct.content + "</div></div></li>");	
					}
				} else {
					$("#blog-comment").html("<h2><b>服务器异常!</b></h2>");
				}
			}
		});
	}
}

$(function() {
	firstRequest();
	layui.use(['element', 'jquery', 'form', 'layedit', 'flow'], function() {
		var element = layui.element;
		var form = layui.form;
		var $ = layui.jquery;
		var layedit = layui.layedit;
		var flow = layui.flow;
		//评论的编辑器
		var editIndex = layedit.build('remarkEditor', {
		    height: 150,
		    tool: ['face', '|', 'link'],
		});
		//评论和留言的编辑器的验证
		form.verify({
			content: function(value) {
				value = $.trim(layedit.getContent(editIndex));
				if (value == "") return "请输入内容";
				layedit.sync(editIndex);
			},
			replyContent: function(value) {
				if (value == "") return "请输入内容";
			}
		});

		//回复按钮点击事件
		$('#blog-comment').on('click', '.btn-reply', function() {
			var targetId = $(this).data('targetid'),
				targetName = $(this).data('targetname'),
				$container = $(this).parent('p').parent().siblings('.replycontainer');
			if ($(this).text() == '回复') {
				$container.find('textarea').attr('placeholder', '回复【' + targetName + '】');
				$container.removeClass('layui-hide');
				$container.find('input[name="targetUserId"]').val(targetId);
				$(this).parents('.blog-comment li').find('.btn-reply').text('回复');
				$(this).text('收起');
			} else {
				$container.addClass('layui-hide');
				$container.find('input[name="targetUserId"]').val(0);
				$(this).text('回复');
			}
		});
		
		//监听提交
		form.on('submit(formLeaveMessage)', function(data) {
			layedit.sync(editIndex);
			$.ajax({
				type: "post",
				url: urls.cliUrl + 'comment',
				async: true,
				data: JSON.stringify(data.field),
				contentType: "application/json; charset=utf-8",
				success: function(res) {
					if (res.errorCode === 0) {
						layer.msg(res.description, {
							icon: 6
						}, function() {
							// 刷新当前页面
							window.location.reload();
						});
					} else {
						layer.msg(res.description, {
							icon: 5
						});
					}
				}
			});
			return false;
		});
	});
});
